﻿using DotnetSpider.Core;
using DotnetSpider.Core.Pipeline;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zj.DotNetSpiderSample.Models;

namespace Zj.DotNetSpiderSample.Pipelines
{
    public class CNBlogPipeline
        : BasePipeline
    {
        private static object _lock = new object();
        public override void Process(IEnumerable<ResultItems> resultItems, ISpider spider)
        {
            foreach (var item in resultItems)
            {
                var blogs = item.Results["BlogList"] as List<CNBlogEntity>;
                Console.WriteLine($"总共抓取博客{blogs.Count}条。");
                foreach (var blog in blogs)
                {
                    //Console.WriteLine($"标题:{blog.Title},\t,内容:{blog.Content},\t发布日期:{blog.Remark}");
                    lock (_lock)
                    {
                        using (var sw = new StreamWriter($"blogs.json", true, Encoding.Default))
                        {
                            sw.Write($"标题:{blog.Title},\t,内容:{blog.Content},\t发布日期:{blog.Remark}\n");
                        }
                    }
                }
                //var json = JsonConvert.SerializeObject(blogs);
            }
        }
    }
}
